package com.bdit.ydcb;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.baidu.mapapi.service.LocationService;
import com.bdit.ydcb.activity.AreaTaskActivity;
import com.bdit.ydcb.activity.MapActivity;
import com.bdit.ydcb.adapter.MainMenAdapter;
import com.bdit.ydcb.utils.ToastUtils;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener{
    private GridView myGridView;
    boolean isOpen = false;
    private Camera camera;
    // 首次按下返回键时间戳
    private long firstBackPressedTime = 0;
    private LocationService locationService;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.textstyle);
        setContentView(R.layout.activity_main);
        //AppManager.getAppManager().addActivity(this);
        myGridView=(GridView)findViewById(R.id.myGridView);
        myGridView.setAdapter(new MainMenAdapter(MainActivity.this));
        myGridView.setOnItemClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       // AppManager.getAppManager().finishActivity();
    }
    /**
     * 返回键关闭导航
     */
    @Override
    public void onBackPressed() {

        long secondBackPressedTime = System.currentTimeMillis();
        if (secondBackPressedTime - firstBackPressedTime > 2000) {
            ToastUtils.with(this).show("再按一次退出");
            firstBackPressedTime = secondBackPressedTime;
        } else {
            super.onBackPressed();
        }

    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent;
        switch (i) {
            case 0:
                intent = new Intent(MainActivity.this, AreaTaskActivity.class);
                startActivity(intent);
                break;
            case 1:
                intent = new Intent(MainActivity.this, MapActivity.class);
                startActivity(intent);
                break;
            case 2:
                ImageView imageView= (ImageView) view.findViewById(R.id.MainActivityImage);
                if(!isOpen){
                    camera = Camera.open();
                    android.hardware.Camera.Parameters params = camera.getParameters();
                    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(params);
                    camera.startPreview(); // 开始亮灯
                    imageView.setImageResource(R.drawable.light_open);
                    isOpen = true;
                }else{
                    camera.stopPreview(); // 关掉亮灯
                    camera.release(); // 关掉照相机
                    imageView.setImageResource(R.drawable.light_close);
                    isOpen=false;
                }
                break;
            default:
                Toast.makeText(MainActivity.this,"正在努力编码中....",Toast.LENGTH_SHORT).show();
        };
    }

}
